package aes.gui;

import java.awt.CardLayout;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;

import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JSeparator;

import aes.general.TeacherControl;

/**
 * The Class TeacherLockPanel. This panel is allowing teacher to lock an ongoing  
 * exam. This panel provides the list of active exams
 * and the ability to block every student from doing
 * the exam and also disable the option for other
 * students to allow it. 
 */
public class TeacherLockPanel extends JPanel {

	
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private JPanel jp;
	private JPanel main;
	private JPanel Warning;
	private JLabel l1;
	private JLabel l2;
	private JButton b1;
	private JButton b2;
	private JButton b3;
	private JButton btnYes;
	private JButton btnNo;
	private JSeparator separator;
	private JComboBox cb;
	
	private boolean flag = false;
	private boolean warning = false;
	private ArrayList<String> active = new ArrayList<String>();
	private String ExamToLock; 
	
	/**
	 * Create the panel.
	 *
	 * @param contentPane the content pane
	 */
	public TeacherLockPanel(JPanel contentPane) {
		this.jp = contentPane;
		initialize();
	}
	
	/**
	 * Initialize.
	 */
	private void initialize() {
		setLayout(null);
		
		l1 = new JLabel("Lock Exam");
		l1.setFont(new Font("Tempus Sans ITC", Font.BOLD, 21));
		l1.setBounds(255, 11, 129, 29);
		add(l1);
		
		separator = new JSeparator();
		separator.setBounds(10, 43, 620, 2);
		add(separator);
		
		cb = new JComboBox();
		cb.setBounds(231, 103, 83, 20);
		add(cb);
		cb.addItem("-------");
		
		
		l2 = new JLabel("Choose Exam To Lock");
		l2.setBounds(37, 106, 155, 14);
		add(l2);
		
		b3 = new JButton("Get Exams");
		b3.setBounds(341, 102, 107, 23);
		add(b3);
		
		main = new JPanel();
		main.setBounds(62, 376, 499, 72);
		add(main);
		main.setLayout(null);
		
		b1 = new JButton("Cancel");
		b1.setBounds(268, 24, 84, 23);
		main.add(b1);
		b1.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				warning = false;
				flag = false;
				main.setVisible(true);
				Warning.setVisible(false);
				CardLayout cl = (CardLayout) (jp.getLayout());
				 cl.show(jp, "mainPanel");
			}
		});
		
		b2 = new JButton("Lock Exam");
		b2.setBounds(141, 24, 105, 23);
		main.add(b2);
		b2.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
	        	if(cb.getSelectedItem() == "-------")
	        		JOptionPane.showMessageDialog(null,
							"Error, No Exam Is Chosen", "Error",
							JOptionPane.ERROR_MESSAGE);
	        	
	        	else if(warning == false){
				JOptionPane.showMessageDialog(null,
						"Warning, You Are About To Lock The Chosen Exam, Are Sure?", "Warning",
						JOptionPane.WARNING_MESSAGE);
				warning = true;
				Warning.setVisible(true);
				
	        	main.setVisible(false);
	        	
	        	}
				
			}
		});
				
				Warning = new JPanel();
				Warning.setBounds(62, 376, 499, 72);
				add(Warning);
				Warning.setLayout(null);
				Warning.setVisible(false);
				
				btnYes = new JButton("Yes");
				btnYes.setBounds(182, 22, 65, 23);
				Warning.add(btnYes);
				btnYes.addActionListener(new ActionListener() {
					public void actionPerformed(ActionEvent arg0) {
						warning = false;
						flag = false;

						JOptionPane.showMessageDialog(null,
								"Exam, Has Been Locked!!!", "Locked",
								JOptionPane.INFORMATION_MESSAGE);

						ExamToLock = (String) cb.getSelectedItem();
						TeacherControl.LockExam(ExamToLock);
						
						CardLayout cl = (CardLayout) (jp.getLayout());
						cl.show(jp, "mainPanel");
						Warning.setVisible(false);
						main.setVisible(true);
						cb.removeAllItems();
						cb.addItem("-------");
						active.clear();
					}
				});
				
				btnNo = new JButton("No");
				btnNo.setBounds(268, 22, 65, 23);
				Warning.add(btnNo);
		btnNo.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				warning = false;
				Warning.setVisible(false);
				main.setVisible(true);
			}
		});
		
		b3.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {	
				if(flag == false){
					TeacherControl.RequestExams(active);
					try {
						Thread.sleep(600);
					} catch (InterruptedException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
					active = TeacherControl.getRunningExams(); 
					for (int i=0;i<active.size();i++)
						cb.addItem(active.get(i));
					flag = true;
				}
			}
		});
	}
}
